package com.yt.base.controller.app;

import com.yt.app.core.interfaces.StudentService;
import com.yt.app.core.model.Activity;
import com.yt.app.core.model.Student;
import com.yt.base.controller.core.BaseController;
import com.yt.core.MsgBean;
import com.yt.core.dao.PagingBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

@Controller
@RequestMapping("/app/student")
public class StudentController extends BaseController {
    @Autowired
    private StudentService studentService;
    @RequestMapping("list")
    public ModelAndView list(@RequestParam(value = "start", required = false) String start,
                             @RequestParam(value = "limit", required = false) String limit){

        ModelAndView mav = new ModelAndView("/app/student/list");
        QueryFilter filter = new DynamicSQLFilterImpl();
        filter.setSortField("id", true);
        PagingBean page = new PagingBean();
        int currentPage = 0;
        int pageSize = 3;
        if (StringUtils.isNotEmpty(limit) && StringUtils.isNumeric(limit)) {
            pageSize = Integer.parseInt(limit);
        }
        if (StringUtils.isNotEmpty(start) && StringUtils.isNumeric(start)) {
            currentPage = (Integer.parseInt(start) - 1) * pageSize;
        }
        page.setPageSize(pageSize);
        page.setStart(currentPage);

        List<Student> list = studentService.find(filter);

        int total = studentService.count(filter);
        mav.addObject("total", total);
        mav.addObject("list", list);
        return mav;
    }
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mav = new ModelAndView("/app/student/add");
        return mav;
    }
    @RequestMapping("save")
    public
    @ResponseBody
    Object save(@RequestParam(value = "id", required = false) Long id,
                @RequestParam(value = "name", required = false) String name,
                @RequestParam(value = "age", required = false) int age,
                @RequestParam(value = "sex", required = false) char sex
                ) {
        MsgBean msg = new MsgBean();
        try {
            Student student = null;
            if (id != null && id > 0) {
                student = studentService.get(id);
                student.setId(id);
                student.setName(name);
                student.setAge(age);
                student.setSex(sex);
            }
            if (student==null){
                student=new Student();
                student.setId(id);
                student.setName(name);
                student.setAge(age);
                student.setSex(sex);
            }
//            if (Strings.isNullOrEmpty(name)) {
//                msg.setFailure(1, "名字不能为空！");
//                return msg.returnMsg();
//            } else if (name.length() > 20) {
//                msg.setFailure(1, "名字不能超过20个字符！");
//                return msg.returnMsg();
//            }
            if (student.getId() != null && student.getId() > 0) {
                studentService.update(student);
            } else {
                studentService.insert(student);
            }
        } catch (Exception e) {
            msg.failure("操作失败，请检查参数是否合理！");
        }
        return msg.returnMsg();
    }
    @RequestMapping("toEdit")
    public ModelAndView toEdit(@RequestParam(value = "id", required = true) Long id) {
        ModelAndView mav = new ModelAndView("/app/student/add");
        Student student = studentService.get(id);
        mav.addObject("obj", student);
        return mav;
    }
    @RequestMapping("/multiDel")
    @ResponseBody
    public Object multiDel(@RequestParam("ids") String ids) {
        MsgBean msg = new MsgBean();
        String[] idArr = ids.split(",");
        if (ids != null) {
            for (String id : idArr) {
                studentService.remove(Long.valueOf(id));
            }
        }
        return msg.returnMsg();
    }
}
